<template>
	<div class="layout-padding">
    <el-card shadow="hover" :body-style="{ paddingBottom: 0 }">
      <el-form :model="state.tableData.param" label-position="top" ref="queryFormRef" :inline="true" label-width="70px">
        <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="state.tableData.param.name" placeholder="请输入姓名" style="width: 100%" @keyup.enter="handleQuery"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="卡号" prop="cardNo">
              <el-input v-model="state.tableData.param.cardNo" placeholder="请输入卡号" style="width: 200px" @keyup.enter="handleQuery"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="单位" prop="workUnit">
              <el-input v-model="state.tableData.param.workUnit" placeholder="请输入单位" style="width: 200px" @keyup.enter="handleQuery"  clearable />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="工作地点" prop="workPlace">
              <el-input v-model="state.tableData.param.workPlace" placeholder="请输入工作地点" style="width: 200px" @keyup.enter="handleQuery"  clearable />
            </el-form-item>
          </el-col>
        </el-row>

          <el-form-item class="button-group">
            <el-button @click="resetQueryForm()">
              <el-icon>
                <ele-Refresh />
              </el-icon>
              重置
            </el-button>
            <el-button type="primary" @click="handleQuery">
              <el-icon>
                <ele-Search />
              </el-icon>
              查询
            </el-button>
            <el-button type="primary" @click="openEditDialog()" v-permission="['system_dict_add']"  plain>
              <el-icon>
                <ele-Plus />
              </el-icon>
              新增
            </el-button>
          </el-form-item>

      </el-form>
    </el-card>
<!--		<el-card shadow="hover" :body-style="{ paddingBottom: 0 }">-->
<!--			<el-form :model="state.tableData.param" ref="queryFormRef" :inline="true" label-width="70px">-->
<!--        <el-form-item label="姓名" prop="name">-->
<!--          <el-input v-model="state.tableData.param.name" placeholder="请输入姓名" style="width: 200px" @keyup.enter="handleQuery"  clearable />-->
<!--        </el-form-item>-->
<!--				<el-form-item label="卡号" prop="cardNo">-->
<!--					<el-input v-model="state.tableData.param.cardNo" placeholder="请输入卡号" style="width: 200px" @keyup.enter="handleQuery"  clearable />-->
<!--				</el-form-item>-->
<!--        <el-form-item label="单位" prop="workUnit">-->
<!--          <el-input v-model="state.tableData.param.workUnit" placeholder="请输入单位" style="width: 200px" @keyup.enter="handleQuery"  clearable />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="工作地点" prop="workPlace">-->
<!--          <el-input v-model="state.tableData.param.workPlace" placeholder="请输入工作地点" style="width: 200px" @keyup.enter="handleQuery"  clearable />-->
<!--        </el-form-item>-->
<!--				<el-form-item class="button-group">-->
<!--					<el-button @click="resetQueryForm()">-->
<!--						<el-icon>-->
<!--							<ele-Refresh />-->
<!--						</el-icon>-->
<!--						重置-->
<!--					</el-button>-->
<!--					<el-button type="primary" @click="handleQuery">-->
<!--						<el-icon>-->
<!--							<ele-Search />-->
<!--						</el-icon>-->
<!--						查询-->
<!--					</el-button>-->
<!--          <el-button type="primary" @click="openEditDialog()" v-permission="['system_dict_add']"  plain>-->
<!--            <el-icon>-->
<!--              <ele-Plus />-->
<!--            </el-icon>-->
<!--            新增-->
<!--          </el-button>-->
<!--				</el-form-item>-->
<!--			</el-form>-->
<!--		</el-card>-->
    <el-card shadow="hover" class="table-card" style="margin-top: 10px">
      <div class="table-container">
        <el-table
            :data="state.tableData.data"
            v-loading="state.tableData.loading"
            style="width: 100%"
            size="default"
            stripe
        >
          <el-table-column prop="name" label="姓名" show-overflow-tooltip></el-table-column>
          <el-table-column prop="cardNo" label="卡号" show-overflow-tooltip></el-table-column>
          <el-table-column prop="workUnit" label="工作单位" show-overflow-tooltip></el-table-column>
          <el-table-column prop="workPlace" label="工作地点" show-overflow-tooltip></el-table-column>
          <el-table-column prop="createdAt" label="创建时间" show-overflow-tooltip></el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <template #default="scope">
              <el-button type="text" size="small" @click="openEditDialog(scope.row)" v-permission="['system_dict_update']">修改</el-button>
              <el-button type="text" size="small" @click="handleDel(scope.row)" v-permission="['system_dict_delete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination-container">
        <el-pagination
            class="pagination-right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :pager-count="5"
            :page-sizes="[10, 20, 30]"
            v-model:current-page="state.tableData.param.pageNum"
            background
            v-model:page-size="state.tableData.param.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="state.tableData.total"
        ></el-pagination>
      </div>
    </el-card>
<!--		<el-card shadow="hover" style="margin-top: 10px">-->
<!--			<el-table :data="state.tableData.data" v-loading="state.tableData.loading">-->
<!--				<el-table-column prop="name" label="姓名" show-overflow-tooltip></el-table-column>-->
<!--				<el-table-column prop="cardNo" label="卡号" show-overflow-tooltip></el-table-column>-->
<!--				<el-table-column prop="workUnit" label="工作单位" show-overflow-tooltip></el-table-column>-->
<!--				<el-table-column prop="workPlace" label="工作地点" show-overflow-tooltip></el-table-column>-->
<!--				<el-table-column prop="createdAt" label="创建时间" show-overflow-tooltip></el-table-column>-->
<!--				<el-table-column label="操作" width="150">-->
<!--					<template #default="scope">-->
<!--						<el-button text type="primary" @click="openEditDialog(scope.row)" v-permission="['system_dict_update']">修改</el-button>-->
<!--						<el-button text type="primary" @click="handleDel(scope.row)" v-permission="['system_dict_delete']">删除</el-button>-->
<!--					</template>-->
<!--				</el-table-column>-->
<!--			</el-table>-->
<!--			<el-pagination-->
<!--				@size-change="handleSizeChange"-->
<!--				@current-change="handleCurrentChange"-->
<!--				:pager-count="5"-->
<!--				:page-sizes="[10, 20, 30]"-->
<!--				v-model:current-page="state.tableData.param.pageNum"-->
<!--				background-->
<!--				v-model:page-size="state.tableData.param.pageSize"-->
<!--				layout="total, sizes, prev, pager, next, jumper"-->
<!--				:total="state.tableData.total"-->
<!--			>-->
<!--			</el-pagination>-->
<!--		</el-card>-->
		<EditDialog ref="editFormRef" @refresh="handleQuery()" />
	</div>
</template>
<script setup lang="ts" name="systemRole">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import dictApi from '/@/api/system/dict';
import worker from "/@/api/manager/worker";

// 引入组件
const EditDialog = defineAsyncComponent(() => import('./component/editDialog.vue'));

// 定义变量内容
const queryFormRef = ref();
const editFormRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
      name: '',
      cardNo: '',
      workUnit: '',
      workPlace: ''
    },
	},
});

// 页面加载时
onMounted(() => {
	handleQuery();
});

// 重置
const resetQueryForm = () => {
	queryFormRef.value?.resetFields();
	handleQuery();
};

// 初始化表格数据
const handleQuery = () => {
	state.tableData.loading = true;
	worker.query(state.tableData.param).then((res) => {
		state.tableData.data = res.data.list;
		state.tableData.total = res.data.totalCount;
		state.tableData.loading = false;
	});
};

// 打开编辑弹窗
const openEditDialog = (row?: any) => {
	editFormRef.value.openDialog(row);
};

// 删除
const handleDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除该员工：“${row.name}”?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			worker.delete({ id: row.id }).then((res) => {
				if (res.success) {
					handleQuery();
					ElMessage.success('删除成功');
				}
			});			
		})
		.catch(() => {});
};

// 分页改变
const handleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	handleQuery();
};

// 分页改变
const handleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	handleQuery();
};
</script>

<style scoped>


.button-group {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}



.table-card {
  padding: 16px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
}

.table-container {
  overflow-x: auto;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  padding: 16px;
}

.pagination-right {
  margin-left: auto;
}
</style>

